package com.easyhuan.action;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.easyhuan.pojo.Goods;
import com.easyhuan.pojo.Picture;
import com.easyhuan.pojo.User;
import com.easyhuan.util.HibernateUtil;

public class GoodsPublish {
	private File[] image;
	private String[] imageFileName;
	private String[] imageContentType;
	private Goods goods;
	private Picture picture;

	public File[] getImage() {
		return image;
	}

	public void setImage(File[] image) {
		this.image = image;
	}

	public String[] getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String[] imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String[] getImageContentType() {
		return imageContentType;
	}

	public void setImageContentType(String[] imageContentType) {
		this.imageContentType = imageContentType;
	}

	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	private final HttpSession session = ServletActionContext.getRequest()
			.getSession();

	/**
	 * @return
	 * @throws IOException
	 */
	public String execute() throws IOException {
		User user = (User) session.getAttribute("currUser");
		System.out.println("User:" + user);
		try {
			Session session1 = HibernateUtil.getHibernateSession();
			Transaction trans = session1.beginTransaction();
			upload(session1, trans, user, goods);// 上传文件并向数据库添加纪录
			return "success";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "republic";
		}
	}

	// 上传图片
	private void upload(Session session, Transaction transaction, User user,
			Goods goods) throws Exception {
		Set<Picture> pictureSet = new HashSet<Picture>();
		// 得到物理路径
		String realPath = ServletActionContext.getServletContext().getRealPath(
				"/uploadimage");

		System.out.println("物理路径：" + realPath);

		// 创建该路径
		File path = new File(realPath);
		// 判断是否存在该路径，如果不存在，创建之
		if (!path.exists()) {
			path.mkdirs();
		}

		changeImageNames();
		// 将多个文件写到服务器硬盘
		for (int i = 0; i < image.length; i++) {
			File file = new File(path, imageFileName[i]);
			FileUtils.copyFile(image[i], file);

			Timestamp time = getCurrentTime();// 获取系统当前时间
			picture = new Picture(time, imageFileName[i], goods);
			pictureSet.add(picture);
			session.save(picture);
			System.out.println("imageContentType:" + imageContentType[i]);
		}
		goods.setPictures(pictureSet);
		goods.setGoodsStatus(0);// 表示从未有人购买
		goods.setUser(user);// 商品所属的用户
		session.save(goods);

		transaction.commit();
		session.close();
	}

	// 更改图片名称
	private void changeImageNames() {
		for (int i = 0; i < imageFileName.length; i++) {
			String name = imageFileName[i].substring(0,
					imageFileName[i].lastIndexOf("."));
			String imageType = imageFileName[i].substring(imageFileName[i]
					.lastIndexOf("."));
			String ins = String.valueOf(System.currentTimeMillis());
			imageFileName[i] = name + ins + imageType;
		}
	}

	// 获取系统当前时间
	private Timestamp getCurrentTime() {
		Calendar calender = Calendar.getInstance();// 使用默认时区和语言环境获得一个日历
		Date tasktime = calender.getTime();// 将Calendar类型转换成Date类型
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期输出的格式
		System.out.println(df.format(tasktime));// 格式化输出
		Timestamp time = Timestamp.valueOf(df.format(tasktime));
		return time;
	}
}